package com.service.impl;

import com.dao.DeviceBoxMapper;
import com.dao.ProductMapper;
import com.model.DeviceBox;
import com.model.Product;
import com.po.DeviceBoxModifyPO;
import com.result.Result;
import com.service.DeviceBoxService;
import com.vo.DeviceBoxQueryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: chihaojie
 * @Date: 2019/5/29 15:57
 * @Version 1.0
 * @Note
 */
@Slf4j
@Service
public class DeviceBoxServiceImpl implements DeviceBoxService {


    @Autowired
    private DeviceBoxMapper deviceBoxMapper;

    @Autowired
    private ProductMapper productMapper;


    @Override
    public Result<Boolean> modifyById(DeviceBoxModifyPO deviceBoxModifyPO) {
        if (StringUtils.isEmpty(deviceBoxModifyPO.getId())){
            return new Result<>().error("仓位ID为空");
        }
        DeviceBox deviceBox = new DeviceBox();
        BeanUtils.copyProperties(deviceBoxModifyPO,deviceBox);
        System.out.println(deviceBox);
        deviceBoxMapper.updateByPrimaryKeySelective(deviceBox);
        return new Result<>().ok();
    }

    @Override
    public Result<List<DeviceBoxQueryVO>> queryAll(DeviceBox deviceBox) {
        List<DeviceBox> deviceBoxes = deviceBoxMapper.selectAll(deviceBox);
        Map<Long,List<DeviceBox>> data = new HashMap<>();
        for (DeviceBox box : deviceBoxes){
            if (box.getProductId()!=null && box.getProductId() !=0){
                List<DeviceBox> subData = data.get(box.getProductId());
                if (subData == null){
                    subData = new ArrayList<>();
                    data.put(box.getProductId(),subData);
                }
                subData.add(box);
            }else{
                List<DeviceBox> subData = data.get(Long.valueOf(-1));
                if (subData == null){
                    subData = new ArrayList<>();
                    data.put(Long.valueOf(-1),subData);
                }
                subData.add(box);
            }
        }
        List<DeviceBoxQueryVO> result = new ArrayList<>();
        for (Map.Entry<Long,List<DeviceBox>> entry:data.entrySet()){
            if (entry.getKey() ==-1){
                for (DeviceBox box : entry.getValue()){
                    DeviceBoxQueryVO deviceBoxQueryVO = new DeviceBoxQueryVO();
                    BeanUtils.copyProperties(box,deviceBoxQueryVO);
                    result.add(deviceBoxQueryVO);
                }
            }else{
                Product product = productMapper.selectByPrimaryKey(entry.getKey());
                for (DeviceBox box : entry.getValue()){
                    DeviceBoxQueryVO deviceBoxQueryVO = new DeviceBoxQueryVO();
                    BeanUtils.copyProperties(box,deviceBoxQueryVO);
                    deviceBoxQueryVO.setProduct(product);
                    result.add(deviceBoxQueryVO);
                }
            }

        }
        return new Result<List<DeviceBoxQueryVO>>().ok(result);
    }

    @Override
    public Result<DeviceBoxQueryVO> queryById(Long id) {
        if (StringUtils.isEmpty(id)){
            return new Result<>().error("仓位ID为空");
        }
        DeviceBox box = deviceBoxMapper.selectByPrimaryKey(id);
        if (box!=null){
            DeviceBoxQueryVO deviceBoxQueryVO = new DeviceBoxQueryVO();
            BeanUtils.copyProperties(box,deviceBoxQueryVO);
            if (!StringUtils.isEmpty(box.getProductId())){
                Product product = productMapper.selectByPrimaryKey(box.getProductId());
                if (product!=null){
                    deviceBoxQueryVO.setProduct(product);
                }
            }
            return new Result<DeviceBoxQueryVO>().ok(deviceBoxQueryVO);
        }
        return new Result<>().ok();
    }
}
